package com.amumu.drama.common.config.redis;

import java.util.Objects;

/**
 * jedis的限流器配置
 * Created by freethinker on 2020-07-02.
 */
public class JedisRateLimiterConfig {

    /** 限流器实例名 */
    private String limitRateName;

    /** 时间段内限制个数 */
    private int limitCount;

    /** 时间段，单位：秒 */
    private int limitSpanSeconds;

    public JedisRateLimiterConfig() {
    }

    public JedisRateLimiterConfig(String limitRateName, int limitCount, int limitSpanSeconds) {
        this.limitRateName = limitRateName;
        this.limitCount = limitCount;
        this.limitSpanSeconds = limitSpanSeconds;
    }

    public static JedisRateLimiterConfig create(String limitRateName, int limitCount, int limitSpanSeconds) {
        return new JedisRateLimiterConfig(limitRateName, limitCount, limitSpanSeconds);
    }

    public String getLimitRateName() {
        return limitRateName;
    }

    public void setLimitRateName(String limitRateName) {
        if (limitRateName == null || limitRateName.isEmpty()) {
            throw new RuntimeException("limitRateName cant be empty.");
        }
        this.limitRateName = limitRateName;
    }

    public int getLimitCount() {
        return limitCount;
    }

    public void setLimitCount(int limitCount) {
        if (limitCount <= 0) {
            throw new RuntimeException("limitCount must be positive number.");
        }
        this.limitCount = limitCount;
    }

    public int getLimitSpanSeconds() {
        return limitSpanSeconds;
    }

    public void setLimitSpanSeconds(int limitSpanSeconds) {
        if (limitSpanSeconds <= 0) {
            throw new RuntimeException("limitSpanSeconds must be positive number.");
        }
        this.limitSpanSeconds = limitSpanSeconds;
    }

    @Override
    public int hashCode() {
        int result = limitRateName != null ? limitRateName.hashCode() : 0;
        result = 31 * result + limitCount;
        result = 31 * result + limitSpanSeconds;
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        JedisRateLimiterConfig that = (JedisRateLimiterConfig) o;

        if (limitCount != that.limitCount) {
            return false;
        }
        if (limitSpanSeconds != that.limitSpanSeconds) {
            return false;
        }
        return Objects.equals(limitRateName, that.limitRateName);
    }

    @Override
    public String toString() {
        return "JedisRateLimiterConfig{" + "limitRateName='" + limitRateName + '\'' + ", limitCount=" + limitCount + ", limitSpanSeconds=" + limitSpanSeconds + '}';
    }
}
